var SysBlackWhiteListDatagrid = new HgUi.Datagrid("SysBlackWhiteList_datagrid");
SysBlackWhiteListDatagrid.toolbar = $("#SysBlackWhiteList_toolbar");
SysBlackWhiteListDatagrid.mode = "rowEdit"; //行编辑模式
SysBlackWhiteListDatagrid.options.isShowContextMenu = true;
SysBlackWhiteListDatagrid.options.tipCells 
	= ["listType","ipRangeStart","ipRangeEnd",
	   "crtType","scenarioType","timeLimitType",
	   "beginTime","endTime","sortNo","descr","isValid","lastExpireTime","crtTime"];
SysBlackWhiteListDatagrid.options.onSelect = function(index,row) {
    if (row && row.dictGroupId){
        SysBlackWhiteListDatagrid.selectedRow = row;
    }
};
SysBlackWhiteListDatagrid.options.onLoadSuccess = function(data) {
    SysBlackWhiteListDatagrid.grid.datagrid("selectRow",0);
};
SysBlackWhiteListDatagrid.init();
SysBlackWhiteListDatagrid.grid.datagrid("enableFilter",[
	{
		field: 'listType',
		type: 'combobox',
		options: {
			panelHeight:'auto',
			editable:false,
			prompt: '---请选择---',
			data:HgUtil.getDictComboboxData('blackOrWhite'),
			onChange: function(value) {
				if (value == ''){
					SysBlackWhiteListDatagrid.grid.datagrid('removeFilterRule', 'listType');
				}
				else{
					SysBlackWhiteListDatagrid.grid.datagrid('addFilterRule', {
						field: 'listType',
						op: 'equal',
						value: value
					});
				}
				SysBlackWhiteListDatagrid.grid.datagrid('doFilter');
			}
		}
	},
	{
		field: 'crtType',
		type: 'combobox',
		options: {
			panelHeight:'auto',
			editable:false,
			prompt: '---请选择---',
			data:HgUtil.getDictComboboxData('crtTypeBlackWhiteList'),
			onChange: function(value) {
				if (value == ''){
					SysBlackWhiteListDatagrid.grid.datagrid('removeFilterRule', 'crtType');
				}
				else{
					SysBlackWhiteListDatagrid.grid.datagrid('addFilterRule', {
						field: 'crtType',
						op: 'equal',
						value: value
					});
				}
				SysBlackWhiteListDatagrid.grid.datagrid('doFilter');
			}
		}
	},
	{
		field: 'scenarioType',
		type: 'combobox',
		options: {
			panelHeight:'auto',
			editable:false,
			prompt: '---请选择---',
			data:HgUtil.getDictComboboxData('scenarioType'),
			onChange: function(value) {
				if (value == ''){
					SysBlackWhiteListDatagrid.grid.datagrid('removeFilterRule', 'scenarioType');
				}
				else{
					SysBlackWhiteListDatagrid.grid.datagrid('addFilterRule', {
						field: 'scenarioType',
						op: 'equal',
						value: value
					});
				}
				SysBlackWhiteListDatagrid.grid.datagrid('doFilter');
			}
		}
	},
	{
		field: 'timeLimitType',
		type: 'combobox',
		options: {
			panelHeight:'auto',
			editable:false,
			prompt: '---请选择---',
			data:HgUtil.getDictComboboxData('timeLimitType'),
			onChange: function(value) {
				if (value == ''){
					SysBlackWhiteListDatagrid.grid.datagrid('removeFilterRule', 'timeLimitType');
				}
				else{
					SysBlackWhiteListDatagrid.grid.datagrid('addFilterRule', {
						field: 'timeLimitType',
						op: 'equal',
						value: value
					});
				}
				SysBlackWhiteListDatagrid.grid.datagrid('doFilter');
			}
		}
	},
	{
		field: 'isValid',
		type: 'combobox',
		options: {
			panelHeight:'auto',
			editable:false,
			prompt: '---请选择---',
			data:HgUtil.getDictComboboxData('trueOrFalse'),
			onChange: function(value) {
				if (value == ''){
					SysBlackWhiteListDatagrid.grid.datagrid('removeFilterRule', 'isValid');
				}
				else{
					SysBlackWhiteListDatagrid.grid.datagrid('addFilterRule', {
						field: 'isValid',
						op: 'equal',
						value: value
					});
				}
				SysBlackWhiteListDatagrid.grid.datagrid('doFilter');
			}
		}
	}
]);
SysBlackWhiteListDatagrid.bindDefaultToolbarEvent();

// --------------------------保存数据------------------------------------------------------------------
SysBlackWhiteListDatagrid.toolbar.find("[tag=save]").click(function() {
    if (SysBlackWhiteListDatagrid.isEditing == true) {
        if (SysBlackWhiteListDatagrid.grid.datagrid("validateRow", SysBlackWhiteListDatagrid.editingIndex)) {
        	var data = SysBlackWhiteListDatagrid.makeSubmitData();
        	if(!SysBlackWhiteListDatagrid.checkData(data)){
        		return;
        	}        	
            var editRow = SysBlackWhiteListDatagrid.grid.datagrid("getRows")[SysBlackWhiteListDatagrid.editingIndex];
            var submitUrl = "/sys/sysBlackWhiteList/add";
            if (editRow.id) {
                submitUrl = "/sys/sysBlackWhiteList/upd";
                data.id = editRow.id;
            }
            if (!HgUtil.repeatSubmitCheck($(this), submitUrl)){
				return;
			}
            HgUtil.post(submitUrl, data, function(data) {
                if (!data.success) {
                    SysBlackWhiteListDatagrid.showMsgbox("error", data.data, 330);
                } else {
                    SysBlackWhiteListDatagrid.grid.datagrid('reload');
                }
            });

        } else {
            SysBlackWhiteListDatagrid.showMsgbox("alert", "有一条数据未校验通过!", 180);
        }
    }
});

//-------------------------删除-----------------------------------------------------------------------
SysBlackWhiteListDatagrid.toolbar.find("[tag=del]").click(function(){
    var row = SysBlackWhiteListDatagrid.grid.datagrid("getSelected");
    if (!row) {
        $.messager.alert("提示","请选择一条数据","warning");
        return;
    }
    $.messager.confirm("确认删除", "确认将选中的数据删除?", function(r){
        if (r) {
            HgUtil.getJson("/sys/sysBlackWhiteList/del",{id:row.id}, function(data){
                if (!data.success) {
                    $.messager.alert("提示","删除失败,"+data.data,"warning");
                } else {
                    HgUi.ok("删除成功!",function(){
                        SysBlackWhiteListDatagrid.grid.datagrid("reload");
                    });
                }
            });
        }
    });
});

//-------------------------置为不在用-----------------------------------------------------------------------
SysBlackWhiteListDatagrid.toolbar.find("[tag=expire]").click(function(){
    var row = SysBlackWhiteListDatagrid.grid.datagrid("getSelected");
    if (!row) {
        $.messager.alert("提示","请选择一条数据","warning");
        return;
    }
    $.messager.confirm("状态更新", "确认将选中的数据置为不在用?", function(r){
        if (r) {
            HgUtil.getJson("/sys/sysBlackWhiteList/expire",{id:row.id}, function(data){
                if (!data.success) {
                    $.messager.alert("提示","更新失败,"+data.data,"warning");
                } else {
                    HgUi.ok("更新成功!",function(){
                        SysBlackWhiteListDatagrid.grid.datagrid("reload");
                    });
                }
            });
        }
    });
});

//-------------------------启用-----------------------------------------------------------------------
SysBlackWhiteListDatagrid.toolbar.find("[tag=on]").click(function(){
    var row = SysBlackWhiteListDatagrid.grid.datagrid("getSelected");
    if (!row) {
        $.messager.alert("提示","请选择一条数据","warning");
        return;
    }
    $.messager.confirm("状态更新", "确认将选中的数据置为启用?", function(r){
        if (r) {
            HgUtil.getJson("/sys/sysBlackWhiteList/on",{id:row.id}, function(data){
                if (!data.success) {
                    $.messager.alert("提示","更新失败,"+data.data,"warning");
                } else {
                    HgUi.ok("更新成功!",function(){
                        SysBlackWhiteListDatagrid.grid.datagrid("reload");
                    });
                }
            });
        }
    });
});

// 组织提交数据
SysBlackWhiteListDatagrid.makeSubmitData = function() {
	debugger;
    var listType = SysBlackWhiteListDatagrid.grid.datagrid('getEditor', {
        index : SysBlackWhiteListDatagrid.editingIndex,
        field : "listType"
    });
    var ipRangeStart = SysBlackWhiteListDatagrid.grid.datagrid('getEditor', {
        index : SysBlackWhiteListDatagrid.editingIndex,
        field : "ipRangeStart"
    });
    var ipRangeEnd = SysBlackWhiteListDatagrid.grid.datagrid('getEditor', {
        index : SysBlackWhiteListDatagrid.editingIndex,
        field : "ipRangeEnd"
    });
    var crtType = SysBlackWhiteListDatagrid.grid.datagrid('getEditor', {
        index : SysBlackWhiteListDatagrid.editingIndex,
        field : "crtType"
    });
    var scenarioType = SysBlackWhiteListDatagrid.grid.datagrid('getEditor', {
        index : SysBlackWhiteListDatagrid.editingIndex,
        field : "scenarioType"
    });
    var timeLimitType = SysBlackWhiteListDatagrid.grid.datagrid('getEditor', {
        index : SysBlackWhiteListDatagrid.editingIndex,
        field : "timeLimitType"
    });
    var beginTime = SysBlackWhiteListDatagrid.grid.datagrid('getEditor', {
        index : SysBlackWhiteListDatagrid.editingIndex,
        field : "beginTime"
    });
    var endTime = SysBlackWhiteListDatagrid.grid.datagrid('getEditor', {
        index : SysBlackWhiteListDatagrid.editingIndex,
        field : "endTime"
    });
    var sortNo = SysBlackWhiteListDatagrid.grid.datagrid('getEditor', {
        index : SysBlackWhiteListDatagrid.editingIndex,
        field : "sortNo"
    });
    var descr = SysBlackWhiteListDatagrid.grid.datagrid('getEditor', {
        index : SysBlackWhiteListDatagrid.editingIndex,
        field : "descr"
    });
    
    var beginTimeVal = $(beginTime.target).val();
    if(beginTimeVal){
    	beginTimeVal = new Date(beginTimeVal);
    }
    var endTimeVal = $(endTime.target).val();
    if(endTimeVal){
    	endTimeVal = new Date(endTimeVal);
    }
    
    var data = {
        listType : $(listType.target).combobox("getValue"),
        ipRangeStart : $(ipRangeStart.target).textbox("getValue"),
        ipRangeEnd : $(ipRangeEnd.target).textbox("getValue"),
        scenarioType : $(scenarioType.target).combobox("getValue"),
        beginTime : beginTimeVal,
        endTime : endTimeVal,
        sortNo : $(sortNo.target).textbox("getValue"),
        descr : $(descr.target).textbox("getValue"),
    };
    return data;
};

SysBlackWhiteListDatagrid.checkData = function(data){
	
	//ip格式
	var ipRegx = "(?:(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(?:(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d))";
	var ipRangeStart = data.ipRangeStart;
	var ipRangeEnd = data.ipRangeEnd;
	if(!ipRangeStart.match(ipRegx) || !ipRangeEnd.match(ipRegx)){
		HgUi.alert("ip不符合格式!");
		return false;
	}
	
	var beginTime = data.beginTime;
	var endTime = data.endTime;
	
	if(beginTime == '' || endTime == ''){
		HgUi.alert("时间不能为空!");
		return false;
	}
	
	if(beginTime > endTime){
		HgUi.alert("开始时间应早于结束时间!");
		return false;
	}
	
	return true;
	
}

// 清空查询---------------------------------------------------------------------------------------------------
SysBlackWhiteListDatagrid.toolbar.find("[tag=clear]").click(function() {
    SysBlackWhiteListDatagrid.grid.datagrid("removeFilterRule");
    SysBlackWhiteListDatagrid.grid.datagrid("load", {});
});


$.extend($.fn.datagrid.defaults.editors, {
    wdate: {//时间控件
        init: function (container, options) {
            var input = $('<input type="text" class="Wdate" onClick="WdatePicker({readOnly:true,dateFmt:\'yyyy-MM-dd HH:mm:ss\'});" />').appendTo(container);
            return input;
        },
        getValue: function (target) {
        	return $(target).val();
        },
        setValue: function (target, value) {
        	if(value){
        		$(target).val(new Date(value).format("yyyy-MM-dd hh:mm:ss"));
        	}else{
        		$(target).val(value);
        	}
        },
        resize: function (target, width) {
            var input = $(target);
            if ($.boxModel == true) {
                input.width(width - (input.outerWidth() - input.width()));
            } else {
                input.width(width);
            }
        }
    }
});